#include <QBrush>
#include <QThread>
#include "track_data_table_model.h"

#include "constant_variable.h"

Track_data_table_model::Track_data_table_model(QObject* parent)
    : QAbstractTableModel(parent)
    , data_list_(new QList<QMap<QString, QString>* > ())
    , data_backup_list_(new QList<QMap<QString, QString>* >())
{
    init_connections();
}

Track_data_table_model::~Track_data_table_model()
{
    if (nullptr != data_backup_list_) {
        delete data_backup_list_;
        data_backup_list_ = nullptr;
    }

    if (nullptr != data_list_) {
        delete data_list_;
        data_list_ = nullptr;
    }
}

int Track_data_table_model::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)

    int row_count(0);

    if ( !data_list_->isEmpty()) {
        row_count = data_list_->count();
    }

    return row_count;
}

int Track_data_table_model::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)

    return 17;
}

QVariant Track_data_table_model::data(const QModelIndex &index, int role) const
{
    if ( !index.isValid()) {
        return QVariant();
    }

    QVariant v = QVariant();
    // 获取对应行处数据序列号
    QMap<QString, QString>* data = data_list_->at(index.row());

    if (Qt::DisplayRole == role) {
        switch (index.column()) {
        case 0:
            if ( data->contains(TRACK_ID)) {
                v = data->value(TRACK_ID);
            } else {
                v = NONE;
            }
            break;

        case 1:
            if ( data->contains(TIME)) {
                v = data->value(TIME);
            } else {
                v = NONE;
            }
            break;

        case 2:
            if ( data->contains(CALL_SIGN)) {
                v = data->value(CALL_SIGN);
            } else {
                v = NONE;
            }
            break;

        case 3:
            if ( data->contains(SSR_CODE)) {
                v = data->value(SSR_CODE);
            } else {
                v = NONE;
            }
            break;

        case 4:
            if ( data->contains(H0)) {
                v = data->value(H0);
            } else if (data->contains("h")) {
                v = (data->value("h") + ", QNH Applied");
            } else {
                v = NONE;
            }
            break;

        case 5:
            if ( data->contains(CFL)) {
                v = data->value(CFL);
            } else {
                v = NONE;
            }
            break;

        case 6:
            if ( data->contains(LONGITUDE)) {
                v = data->value(LONGITUDE);
            } else {
                v = NONE;
            }
            break;

        case 7:
            if ( data->contains(LATITUDE)) {
                v = data->value(LATITUDE);
            } else {
                v = NONE;
            }
            break;

        case 8:
            if ( data->contains(SPEED)) {
                v = data->value(SPEED);
            } else {
                v = NONE;
            }
            break;

        case 9:
            if ( data->contains(HEADING)) {
                v = data->value(HEADING);
            } else {
                v = NONE;
            }
            break;

        case 10:
            if ( data->contains(ARR_AIRPORT)) {
                v = data->value(ARR_AIRPORT);
            } else {
                v = NONE;
            }
            break;

        case 11:
            if ( data->contains(DEP_AIRPORT)) {
                v = data->value(DEP_AIRPORT);
            } else {
                v = NONE;
            }
            break;

        case 12:
            if ( data->contains(EOBT)) {
                v = data->value(EOBT);
            } else {
                v = NONE;
            }
            break;

        case 13:
            if ( data->contains(RVSM)) {
                v = data->value(RVSM);
            } else {
                v = NONE;
            }
            break;

        case 14:
            if ( data->contains(FR)) {
                v = data->value(FR);
            } else {
                v = NONE;
            }
            break;

        case 15:
            if ( data->contains(ACTF_TURB)) {
                v = data->value(ACTF_TURB);
            } else {
                v = NONE;
            }
            break;

        case 16:
            if ( data->contains(TIME_STAMP)) {
                v = data->value(TIME_STAMP);
            } else {
                v = NONE;
            }
            break;
        }

        return v;
    }

    if (Qt::BackgroundRole == role) {
        switch (index.row() % 3) {
        case 0:
            v = QBrush(Qt::yellow, Qt::SolidPattern);
            break;

        case 1:
            v = QBrush(QColor(187, 255, 255), Qt::SolidPattern);
            break;

        case 2:
            v = QBrush(QColor(189, 183, 107), Qt::SolidPattern);
            break;
        }

        return v;
    }

    return QVariant();
}

QVariant Track_data_table_model::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (Qt::DisplayRole == role && Qt::Horizontal == orientation) {
        QString header;

        switch (section) {
        case 0:
            header = TRACK_ID.toUpper();
            break;

        case 1:
            header = TIME.toUpper();
            break;

        case 2:
            header = CALL_SIGN.toUpper();
            break;

        case 3:
            header = SSR_CODE.toUpper();
            break;

        case 4:
            header = H0.toUpper();
            break;

        case 5:
            header = CFL;
            break;

        case 6:
            header = LONGITUDE.toUpper();
            break;

        case 7:
            header = LATITUDE.toUpper();
            break;

        case 8:
            header = SPEED;
            break;

        case 9:
            header = HEADING;
            break;

        case 10:
            header = ARR_AIRPORT;
            break;

        case 11:
            header = DEP_AIRPORT;
            break;

        case 12:
            header = EOBT;
            break;

        case 13:
            header = RVSM;
            break;

        case 14:
            header = FR;
            break;

        case 15:
            header = ACTF_TURB.toUpper();
            break;

        case 16:
            header = TIME_STAMP.toUpper();
            break;
        }

        return header;
    }

    return QAbstractTableModel::headerData(section, orientation, role);
}

void Track_data_table_model::init_connections()
{
    connect(this, &Track_data_table_model::si_get_track_data,
            this, &Track_data_table_model::add_data_to_process_list);

    connect(this, &Track_data_table_model::si_get_reset_model_flag,
            this, &Track_data_table_model::reset_data_set);
}

void Track_data_table_model::add_data_to_process_list(QMap<QString, QString>* map)
{
    data_list_->append(map);
    data_backup_list_->append(map);

    emit layoutChanged();

    // 当列表添加数据后，刷新显示当前行数据。测试无效，待修改
//    int row = data_list_->count() ;
//    emit dataChanged(createIndex(row, 0), createIndex(row, 16));
}

void Track_data_table_model::reset_data_set()
{
    if (nullptr != data_list_) {
        data_list_->clear();
    }
}
